package com.crm.system.controller.cluePoolManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.cluePoolManager.CluePoolCompany;
import com.crm.model.entity.cluePoolManager.CluePoolCompanyExample;
import com.crm.model.entity.cluePoolManager.CluePoolPublic;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.cluePoolManager.*;
import com.crm.model.vo.cluePoolManager.CluePoolPubliclOpenSeaVO;
import com.crm.model.vo.cluePoolManager.CluePoolRuleVO;
import com.crm.model.vo.cluePoolManager.CluePoolUpdateVO;
import com.crm.model.vo.cluePoolManager.CluePoolVO;
import com.crm.system.service.cluePoolManager.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author luojie
 * @date 2020/12/24 11:14
 */
@RestController
@RequestMapping("/cluePoolPublic")
@Slf4j
public class CluePoolPublicController {
    @Autowired
    private CluePoolFormService cluePoolFormService;

    @Autowired
    private CluePoolPublicService cluePoolPublicService;

    @Autowired
    private CluePoolCompanyService cluePoolCompanyService;

    @Autowired
    private CluePoolRuleService cluePoolRuleService;

    /**
     * 线索管理(管理系统)--线索公海表单列表分页信息
     * @Author yincuimei
     * @return
     */
    @PostMapping("/selectCluePoolList")
    public DataResponse selectCluePoolList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                           @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                           @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                           @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                           CluePoolQO cluePoolQO//查询条件
    ){
        try{
            //获取登录人信息
            UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
            DhPageInfo<CluePoolVO> cluePoolVODhPageInfo = null;
            if(CommonUtils.isAdmin(userAccount.getUserId())){//超级管理员
                cluePoolVODhPageInfo = cluePoolFormService.selectAllCluePoolList(cluePoolQO,pageIndex,pageSize,sortName,sortOrder);
            }else{//普通管理员
                cluePoolVODhPageInfo = cluePoolFormService.selectNormalCluePoolList(cluePoolQO,userAccount,pageIndex,pageSize,sortName,sortOrder);
            }
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),cluePoolVODhPageInfo);
        }catch(Exception e){
            log.error("cluePoolPublic selectCluePoolList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     *线索管理(管理系统)--线索公海表单开启/停用线索池(备注:只有全国池、分公司公有池、私有池的线索池才有;个人池没有停用/启用操作)
     * @Author yincuimei
     * @return
     */
    @PostMapping("/updatePoolState")
    public DataResponse updatePoolState(@RequestParam(value="poolId",required = false) String poolId,//线索池ID
                                        @RequestParam(value="poolState",required = false) Integer poolState//停用/启用状态
    ){
        try{
            Boolean result =  cluePoolFormService.updatePoolStateById(poolId,poolState);
            if(result){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }catch(Exception e){
            log.error("cluePoolPublic updatePoolState error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 添加公共线索池
     * @param cluePoolPublicQO
     * @return
     */
    @PostMapping("/addCluePoolPublic")
    public DataResponse addCluePoolPublic(HttpServletRequest request,
                                          @RequestBody CluePoolPublicQO cluePoolPublicQO) {
        try {
                Boolean result = cluePoolPublicService.addCluePoolPublic(cluePoolPublicQO);
                if (result) {
                    return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
                } else {
                    return new DhResponse(ResponseCodeEnum.POOL_NAME_DUPLICATE.getResponseCode());
                }
            }catch(Exception e){
                log.error("cluePoolPublic addCluePoolPublic error:", e);
                return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
           }

    }

    /**
     * @author wll
     * info 用户模块-线索池管理-线索池公海表单
     * @return
     */
    @PostMapping("/openSeaPageList")
    public DataResponse openSeaPageList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,
                                        @RequestParam(value = "pageSize", required = true) Integer pageSize,
                                        @RequestParam(value="sortName",required = false) String sortName,
                                        @RequestParam(value="sortOrder",required = false) String sortOrder,//排序(asc/desc)
                                        CluePoolOpenSeaQO cluePoolOpenSeaQO){//查询条件
        try{
            if(cluePoolOpenSeaQO.getTabType() == 1){//全国
                DhPageInfo<CluePoolPubliclOpenSeaVO>  CluePoolPublicOpenSeas = cluePoolPublicService.getCluePoolPublicInfo(cluePoolOpenSeaQO,pageIndex,pageSize,sortName,sortOrder);
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),CluePoolPublicOpenSeas);
            }else {//公司 我参与的
                UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
                //匹配(线索池公司关系表)
                List<CluePoolCompany> cluePoolCompanies = null;
                CluePoolCompanyExample cluePoolCompanyExample = new CluePoolCompanyExample();
                CluePoolCompanyExample.Criteria criteria = cluePoolCompanyExample.createCriteria();
                if(StringUtils.isNotBlank(userAccount.getCompanyId())) {//查询当前登录人公司id
                    criteria.andCompanyIdEqualTo(userAccount.getCompanyId());
                    criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
                    cluePoolCompanies = cluePoolCompanyService.selectByExample(cluePoolCompanyExample);
                }
                DhPageInfo<CluePoolPubliclOpenSeaVO>  CluePoolCompanyOpenSeas = cluePoolCompanyService.getCluePoolCompanyInfo(cluePoolOpenSeaQO,pageIndex,pageSize,sortName,sortOrder,cluePoolCompanies,userAccount);
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),CluePoolCompanyOpenSeas);
            }

        }catch(Exception e){
            log.error("cluePoolPublic openSeaPageList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }
    /**
     * 根据线索池id查询线索池规则
     * @param poolId
     * @param model
     * @return
     */
    @PostMapping("/getCluePoolRule")
    public DataResponse getCluePoolRule(@RequestParam(value="poolId") String poolId, ModelMap model){
        try{
            CluePoolRuleVO cluePoolRuleVO = cluePoolRuleService.selectCluePoolRuleByCluePoolId(poolId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),cluePoolRuleVO);
        }catch(Exception e){
            e.printStackTrace();
            log.error("cluePoolPublic getCluePoolRule error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 修改线索池规则
     * @param request
     * @param cluePoolRuleQO
     * @return
     */
    @PostMapping("/updateCluePoolRule")
    public DataResponse updateCluePoolRule(HttpServletRequest request,
                                          @RequestBody CluePoolRuleQO cluePoolRuleQO) {
        try {
            Boolean result = cluePoolPublicService.updateCluePoolRule(cluePoolRuleQO);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }catch(Exception e){
            log.error("cluePoolPublic updateCluePoolRule error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }

    }


    /**
     * 线索池权限与详情
     * @param cluePoolPermissionAndDetailQO
     * @return
     */
    @PostMapping("/saveCluePoolPermissionAndDetail")
    public DataResponse saveCluePoolPermissionAndDetail(HttpServletRequest request,
                                          @RequestBody CluePoolPermissionAndDetailQO cluePoolPermissionAndDetailQO) {
        try {
            Boolean result = cluePoolPublicService.saveCluePoolPermissionAndDetail(cluePoolPermissionAndDetailQO);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            } else {
                return new DhResponse(ResponseCodeEnum.POOL_NAME_DUPLICATE.getResponseCode());
            }
        }catch(Exception e){
            log.error("cluePoolPublic saveCluePoolPermissionAndDetail error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }

    }

    /**
     *更新线索池
     * @param request
     * @param cluePoolUpdateQO
     * @return
     */
    @PostMapping("/updateCluePoolPublic")
    public DataResponse updateCluePoolPublic(HttpServletRequest request, @RequestBody CluePoolUpdateQO cluePoolUpdateQO){

        try {
            Boolean result = cluePoolPublicService.updateCluePoolPublic(cluePoolUpdateQO);
            if (result) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            } else {
                return new DhResponse(ResponseCodeEnum.POOL_NAME_DUPLICATE.getResponseCode());
            }
        }catch(Exception e){
            log.error("cluePoolPublic updateCluePoolPublic error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 根据线索池id查询需要编辑的线索池信息
     * @param request
     * @param cluePoolId
     * @return
     */
    @GetMapping("/selectCluePoolUpdateInfo")
    public DataResponse selectCluePoolUpdateInfo(HttpServletRequest request,@RequestParam String cluePoolId){
        CluePoolUpdateVO cluePoolUpdateVO = cluePoolPublicService.selectCluePoolUpdateInfo(cluePoolId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),cluePoolUpdateVO);
    }

    /**
     * 管理端添加线索后跳转线索清单列表
     * @param request
     * @param model
     * @param poolId
     * @param poolPermissionFlagMark
     * @return
     */
    @RequestMapping("/cluePoolManager/jumpPoolDetail")
    public DataResponse jumpPoolDetail(HttpServletRequest request, ModelMap model,@RequestParam(value="poolId") String poolId,@RequestParam(value="poolPermissionFlag") Boolean poolPermissionFlagMark){
        Boolean poolPermissionFlag = false;
        CluePoolPublic cluePoolPublic = cluePoolPublicService.selectCluePoolPublicById(poolId);
        if(cluePoolPublic.getNature()==1 && poolPermissionFlagMark){
            poolPermissionFlag = true;
        }
        String url = "/front/cluePoolManager/cluePoolDetail?poolId=" + poolId + "&poolCategory=" + cluePoolPublic.getNature()+ "&poolState=" + cluePoolPublic.getPoolState() + "&defaultMark=" + cluePoolPublic.getDefaultMark() + "&allocation=" + cluePoolPublic.getAllocation()
                + "&companyId=" + cluePoolPublic.getCompanyId() + "&poolPermissionFlag=" + poolPermissionFlag;
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),url);
    }

}
